﻿using B.T.BasicData.ErrorCode;
using B.T.BasicData.Write_Api.DTO;
using B.T.BasicData.Write_Api.RBAC.IServices;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.T.BasicData.Write_Api.Controllers
{
    /// <summary>
    /// 用户系统管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {

        private readonly ILogger<UserController> logger;
        private readonly IUserServices userServices;

        public UserController(ILogger<UserController> logger, IUserServices userServices)
        {
            this.logger = logger;
            this.userServices = userServices;
        }

        /// <summary>
        /// 创建用户信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public async Task<ApiResult> AddUserinfo(AddUserinfoDto dto)
        {
            try
            {
                return await userServices.AddUserinfo(dto);
            }
            catch (Exception ex)
            {

                logger.LogError("=>" + ex.Message);
                throw ex;
            }
        }

        /// <summary>
        /// 删除用户信息（软删除）
        /// </summary>
        /// <param name="userId">用户编号</param>
        /// <returns>操作结果</returns>
        [HttpDelete]
        [Authorize]
        public async Task<ApiResult> DeleteUser(long userId)
        {
            try
            {
                return await userServices.DeleteUser(userId);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);
                throw ex;
            }
        }


        /// <summary>
        /// 编辑用户信息及其角色
        /// </summary>
        /// <param name="dto">用户信息编辑DTO</param>
        /// <returns>操作结果</returns>
        [HttpPut]
        [Authorize]
        public async Task<ApiResult> UpdateUserinfo(UpdateUserinfoDto dto)
        {
            try
            {
                return await userServices.UpdateUserinfo(dto);
            }
            catch (Exception ex)
            {

                logger.LogError("=>" + ex.Message);
                throw ex;
            }
        }

    }
}
